<template>

    <div>

      <!-- <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
        <el-form-item label="雷达IP" prop="ip">
          <el-input v-model="queryParams.ip" placeholder="请输入雷达IP" clearable style="width: 240px"
            @keyup.enter="handleQuery" />
        </el-form-item>
  
        <el-form-item>
          <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
          <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
        </el-form-item>
      </el-form> -->
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary"  @click="handleAdd" v-hasPermi="['system:post:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success"  :disabled="single" @click="handleUpdate" >
            修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger"  :disabled="multiple" @click="handleDelete" >
            删除
          </el-button>
        </el-col>
      </el-row>
  
      <el-table v-loading="loading"  :data="dataList" border stripe header-cell-class-name="el-table-header-cell" highlight-current-row 
      @selection-change="handleSelectionChange" >
        <el-table-column type="selection" :selectable="selectable" width="55" />
        <el-table-column prop="id" label="编号" align="center" fixed  />
        <el-table-column prop="ip" label="雷达IP" width="180" align="center" :show-overflow-tooltip="true" />
        <el-table-column prop="port" label="雷达端口" width="120" align="center" />
        <el-table-column prop="latitude" label="纬度" width="120" align="center" />
        <el-table-column prop="longitude" label="经度" width="120" align="center" />
        <el-table-column prop="northDeviationAngle" label="北偏角" align="center" />

        <el-table-column prop="defenceRadius" label="波束半径" align="center" />
        <el-table-column prop="defenceAngle" label="波束角度" align="center" />

        <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="statusOptions" :value="scope.row.status" />
        </template>
        </el-table-column>
        <el-table-column label="操作" width="160">
          <template #default="scope">
            <el-button type="success" size="small" icon="edit" title="编辑" 
              @click="handleUpdate(scope.row)"></el-button>
            <el-button type="danger" size="small" icon="delete" title="删除" 
              @click="handleDelete(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" /> -->
  
      <!-- 添加或修改对话框 -->
      <el-dialog :title="title" v-model="open" append-to-body>
        <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
          <el-row :gutter="20">
  
            <el-col :lg="12" v-if="opertype != 1">
              <el-form-item label="编号" prop="id">
                <el-input-number v-model.number="form.id" controls-position="right" placeholder="请输入编号"
                  :disabled="true" />
              </el-form-item>
            </el-col>
            <el-col :lg="12">
              <el-form-item label="IP" prop="ip">
                <el-input v-model="form.ip" placeholder="请输入IP" />
              </el-form-item>
            </el-col>
            <el-col :lg="12">
              <el-form-item label="端口" prop="port">
                <el-input v-model.number="form.port" placeholder="请输入端口" />
              </el-form-item>
            </el-col>
            <el-col :lg="12">
              <el-form-item label="北偏角" prop="northDeviationAngle">
                <el-input v-model="form.northDeviationAngle" placeholder="请输入北偏角" />
              </el-form-item>
            </el-col>
            <el-col :lg="12">
              <el-form-item label="纬度" prop="latitude">
                <el-input v-model="form.latitude" placeholder="请输入纬度" />
              </el-form-item>
            </el-col>
            <el-col :lg="12">
              <el-form-item label="经度" prop="longitude">
                <el-input v-model="form.longitude" placeholder="请输入经度" />
              </el-form-item>
            </el-col>

            <el-col :lg="12">
              <el-form-item label="波束半径" prop="defenceRadius">
                <el-input v-model="form.defenceRadius" placeholder="请输入波束半径" />
              </el-form-item>
            </el-col>
            <el-col :lg="12">
              <el-form-item label="波束角度" prop="defenceAngle">
                <el-input v-model="form.defenceAngle" placeholder="请输入波束角度" />
              </el-form-item>
            </el-col>

            <el-col :lg="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio v-for="dict in statusOptions" :key="dict.dictValue" :label="parseInt(dict.dictValue)">{{
                  dict.dictLabel }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          </el-row>
        </el-form>
        <template #footer v-if="opertype != 3">
          <el-button text @click="cancel">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </template>
      </el-dialog>
    </div>
  </template>
  
<script>
import { listRadar, addRadar, delRadar, updateRadar, getRadar } from '@/api/device/radar'

export default {
  name: 'radar',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      dataList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sort: 'id',
        sortType: 'asc',
        ip: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
         ip: [{ required: true, message: 'ip不能为空', trigger: 'blur' }],
         port: [{ required: true, message: 'port不能为空', trigger: 'blur' }],
      },
    }
  },
  created() {
    this.getList()
    this.getDicts('device_status').then((response) => {
      this.statusOptions = response.data.data
    })
  },
  methods: {
    getList() {
      this.loading = true
      listRadar(this.queryParams).then(res => {
      if (res.data.code == 200) {
        this.dataList = res.data.data
        //total.value = data.totalNum
        this.loading = false
      }
      })
    },
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        bindingAreaId: -1,
        ip: null,
        port: '',
        status:'1',
        latitude: 0,
        longitude: 0,
        northDeviationAngle: 0
      }
      this.resetForm('formRef')
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加相机'
    },
    handleUpdate(row) {
      this.reset()
      const Ids = row.id || this.ids
      getRadar(Ids).then((response) => {
        this.form = response.data.data
        this.open = true
        this.title = '修改相机'
      })
    },
    submitForm: function () {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateRadar(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addRadar(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    handleDelete(row) {
      const Ids = row.id || this.ids
      this.$confirm('是否确认删除雷达编号为"' + Ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return delRadar(Ids)
        })
        .then(() => {
          this.getList()
          proxy.$modal.msgSuccess("删除成功")
        })
    },
  },
}
</script>
  